package com.nd.mobile.study.view;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.constants.SharedPreferenceConstants;
import com.nd.mobile.study.common.util.SharedPreferencesUtil;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class StudyRecordTabActivity extends TabActivity {

	private boolean			isFullScreen	= false;

	SharedPreferencesUtil	sp;

	Intent					mIntent			= null;

	private TextView mTVheaderTitle;
	private ImageButton mHeaderBtnBack;
	protected ImageButton mHeaderBtnHome;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferencesUtil sp = SharedPreferencesUtil.getSyscfgSp(this);
		isFullScreen = sp.getBoolean(SharedPreferenceConstants.SYSCFG_FULLLSCREEN);
		if (sp != null) {
			Log.d("MainActivity onCreate", sp.toString());
		}
		if (isFullScreen) {
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		}
		
		setContentView(R.layout.inner_tab);

		mTVheaderTitle = (TextView) findViewById(R.id.header_title_tv);
		mHeaderBtnBack = (ImageButton) findViewById(R.id.header_btn_back);
		mTVheaderTitle.setText(R.string.tv_app_studyrecord);
		mHeaderBtnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		mHeaderBtnHome = (ImageButton)findViewById(R.id.header_btn_home);
		mHeaderBtnHome.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(StudyRecordTabActivity.this, TabMainActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				finish();
				startActivity(intent);
			}
		});
		
		// 创建TabHost
		TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();

		// 首页
		TabSpec ts_practise = tabHost.newTabSpec("video");
		ts_practise.setIndicator(createIndicator("视频记录"));
		ts_practise.setContent(new Intent(this, VideoRecordCatalogActivity.class));
		tabHost.addTab(ts_practise);

		// 目录
		TabSpec ts_storage = tabHost.newTabSpec("homework");
		ts_storage.setIndicator(createIndicator("作业记录"));
		ts_storage.setContent(new Intent(this, HomeworkRecordCatalogActivity.class));
		tabHost.addTab(ts_storage);

		// 应用
		TabSpec ts_down = tabHost.newTabSpec("bluebook");
		ts_down.setIndicator(createIndicator("套卷记录"));
		Intent downIntent = new Intent(this, BluebookRecordCatalogActivity.class);
		ts_down.setContent(downIntent);
		tabHost.addTab(ts_down);

		// 跳出选中当前进去的tab
		mIntent = getIntent();
		int index = mIntent.getIntExtra("index", -1);
		if (index != -1) {
			tabHost.setCurrentTab(index);
		}
	}
	
	private View createIndicator(String label) {
		// 获取整个标签页的view
		View indicatorView = LayoutInflater.from(this).inflate(R.layout.pager_tab_item, null);
		// 设置文字
		TextView tvLabel = (TextView) indicatorView.findViewById(R.id.pager_tab_label);
		tvLabel.setText(label);
		return indicatorView;
	}
}
